/*
 * Copyright (c) 2018 - 2028. www.vtuzx.com Inc. All Rights Reserved
 */
package com.vtuzx.kingdee.xqc_api.web;
import com.vtuzx.core.VtuzxException;
import com.vtuzx.core.bean.dto.VtuzxToken;
import com.vtuzx.core.constant.IVtuzxConst;
import com.vtuzx.core.utils.VtuzxUtil;
import com.vtuzx.kingdee.xqc_api.service.IXqcPurContractService;
import com.vtuzx.web.constant.ApiType;
import com.vtuzx.web.validator.VtuzxValidator;
import com.vtuzx.web.web.VtuzxApi;
import com.vtuzx.web.web.VtuzxControlParam;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Map;

/**
 * 采购合同
 */
@RequestMapping("/${vtuzx.fast.api.path}/${vtuzx.fast.api.version}/pur_con")
@RestController
public class XqcPurConController {
    /**
     * 采购服务
     */
    @Autowired
    private IXqcPurContractService purContractService;


    /**
     * 保存采购合同单
     */
    @PostMapping("/save_pur_con")
    @VtuzxApi
    public Object savePurCon(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        return purContractService.savePurCon(_userId, name, param);
    }

    /**
     * 删除采购合同单
     */
    @PostMapping("/delete_pur_con")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "billNo", label = "单号", notNull = true)
    } )
    public Object deletePurCon(@SuppressWarnings("unused") @VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        String billNo = VtuzxUtil.getString(param, "billNo");
        purContractService.deletePurCon(billNo);
        return null;
    }

    /**
     * 审核采购收货单
     */
    @PostMapping("/audit_pur_con")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "billNo", label = "单号", notNull = true)
    } )
    public Object auditPurCon(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String billNo = VtuzxUtil.getString(param, "billNo");
        String name = VtuzxUtil.getString(token, "name");
        purContractService.auditPurCon(_userId, name, billNo);
        return null;
    }

    /**
     * 反审核采购收货单
     */
    @PostMapping("/un_audit_pur_con")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "billNo", label = "单号", notNull = true)
    } )
    public Object unAuditPurCon(@SuppressWarnings("unused") @VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        String billNo = VtuzxUtil.getString(param, "billNo");
        purContractService.unAuditPurCon(billNo);
        return null;
    }

    /**
     * 获取采购合同单
     */
    @PostMapping("/get_pur_con_list")
    @VtuzxApi
    public Object getPurConList(@SuppressWarnings("unused") @VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        int pageIndex = VtuzxUtil.getInt(param, IVtuzxConst.Key_pageIndex);
        int pageSize = VtuzxUtil.getInt(param, IVtuzxConst.Key_pageSize, IVtuzxConst.PageSize);
        return purContractService.getPurConList(param, pageIndex, pageSize);
    }

    /**
     * 获取采购合同单详情
     */
    @PostMapping("/get_pur_con")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "billNo", label = "单号", notNull = true)
    } )
    public Object getPurCon(@SuppressWarnings("unused") @VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        String billNo = VtuzxUtil.getString(param, "billNo");
        return purContractService.getPurCon(billNo);
    }

    /**
     * 下载采购合同列表
     */
    @PostMapping("/download_pur_con")
    @VtuzxApi(type = ApiType.Download)
    public Object downloadPurCon(@SuppressWarnings("unused") @VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws Exception {
        return purContractService.downloadPurCon(param);
    }

    /**
     * 下推采购合同单
     */
    @PostMapping("/push_pur_con_order")
    @VtuzxApi(type = ApiType.Download, fields = {
            @VtuzxValidator(key = "billNo", label = "单号", notNull = true)
    } )
    public Object pushPurConOrder(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws Exception {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        purContractService.pushPurConOrder(_userId, name, param);
        return null;
    }

    /**
     * 查询物料(包材)
     */
    @PostMapping("/get_material_list")
    @VtuzxApi
    public Object getMaterialByKey(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        String key = VtuzxUtil.getString(param, "key");
        int pageSize = VtuzxUtil.getInt(param, IVtuzxConst.Key_pageSize);
        return purContractService.getMaterial(token, key, pageSize);
    }
}
